/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cw.ulti;

import cw.entity.Question;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author VinhTC
 */
public class PuzzleUltil {

    public PuzzleUltil() {
    }

    /**
     * getCellsByPosisitonAndDirection
     * @param rowSize - row size of puzzle
     * @param colSize - column size of puzzle
     * @param posX - row position of first character of answer
     * @param posY - column position of first character of answer
     * @param answer - answer of question
     * @param direction - direction of answer
     * @return char[][]
     */
    public char[][] getCellsByPosisitonAndDirection(int rowSize, int colSize, int posX, int posY, String answer, boolean direction) {
        char[][] cellsResult = new char[rowSize][colSize];
        char[] ans = answer.toCharArray();

        if (!direction) {//chieu ngang
            int ansLeng = ans.length;
            int cellFree = colSize - (ansLeng + posY);
            if (cellFree >= 0) {
                for (int i = 0; i < rowSize; i++) {
                    if (i == posX) {
                        for (int j = 0; j < colSize; j++) {
//                            System.out.println("ans: (" + i + ", " + j + ") " + ans[j]);
                            try {
                                cellsResult[posX][posY + j] = ans[j];
                            } catch (Exception e) {
                            }
                        }
                    }
                }
            } else {
                JOptionPane.showMessageDialog(null, "Answer has length longer quantity of free cell of row");
            }
        } else {//chieu doc
            int ansLeng = ans.length;
            int cellFree = rowSize - (ansLeng + posX);

            if (cellFree >= 0) {
                for (int i = 0; i < rowSize; i++) {
                    for (int j = 0; j < colSize; j++) {
                        if (j == posY) {
//                            System.out.println("ans: (" + i + ", " + j + ") " + ans[i]);
                            try {
                                cellsResult[posX + i][posY] = ans[i];
                            } catch (Exception e) {
                            }
                        }
                    }
                }
            } else {
                JOptionPane.showMessageDialog(null, "Answer has length longer quantity of free cell of column");
            }
        }
        return cellsResult;
    }

    /**
     * getDirection
     * @param direct
     * @return boolean
     */
    public boolean getDirection(int direct) {
        if (direct == 0) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * getCellsToStringArr
     * @param ch
     * @param cellsResult
     * @return String[][]
     */
    public String[][] getCellsToStringArr(char[][] ch, String[][] cells) {
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                try {
                    if (cells[i][j] == null || cells[i][j].trim().equals("")) {
                        for (int k = 0; k < ch.length; k++) {
                            for (int l = 0; l < ch[k].length; l++) {
                                try {
                                    if (String.valueOf(ch[k][l]).trim().length() > 0) {
                                        if (k == i && l == j) {
                                            cells[i][j] = String.valueOf(ch[k][l]).toUpperCase();
                                        }
                                    }
                                } catch (Exception e) {
                                    Logger.getLogger(PuzzleUltil.class.getName()).log(Level.SEVERE, null, e);
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    Logger.getLogger(PuzzleUltil.class.getName()).log(Level.SEVERE, null, e);
                }
            }
        }

        return cells;
    }

    /**
     * checkCharArr
     * @param chars 
     * @return boolean
     */
    public boolean checkCharArr(char[][] chars) {
        boolean result = false;
        for (int i = 0; i < chars.length; i++) {
            for (int j = 0; j < chars[i].length; j++) {
                try {
                    if (!String.valueOf(chars[i][j]).trim().equals("")) {
                        result = true;
                    }
                } catch (Exception e) {
                    System.out.println("Error");
                    Logger.getLogger(PuzzleUltil.class.getName()).log(Level.SEVERE, null, e);
                }
            }
        }
        return result;
    }

    /**
     * checkCharExist
     * @param ch
     * @param cells
     * @return boolean
     */
    public boolean checkCharExist(char[][] ch, String[][] cells) {
        boolean result = false;
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                try {
                    if (cells[i][j] != null && !cells[i][j].trim().equals("")) {
                        for (int k = 0; k < ch.length; k++) {
                            for (int l = 0; l < ch[k].length; l++) {
                                try {
                                    if (String.valueOf(ch[k][l]).trim().length() > 0) {
                                        if (k == i && l == j) {
                                            result = true;
                                        }
                                    }
                                } catch (Exception e) {
                                    Logger.getLogger(PuzzleUltil.class.getName()).log(Level.SEVERE, null, e);
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    Logger.getLogger(PuzzleUltil.class.getName()).log(Level.SEVERE, null, e);
                }
            }
        }
        return result;
    }

    /**
     * getCellsOfPuzzleFromDatabase
     * @param quests
     * @param rowSize
     * @param colSize
     * @return String[][]
     */
    public String[][] getCellsOfPuzzleFromDatabase(List<Question> quests, int rowSize, int colSize) {
        try {
            String[][] result = new String[rowSize][colSize];
            PuzzleUltil puzzUltil = new PuzzleUltil();
            char[][] charArr = null;
            Question quest = null;
            for (int i = 0; i < quests.size(); i++) {
                quest = quests.get(i);
                charArr = puzzUltil.getCellsByPosisitonAndDirection(rowSize, colSize,
                        quest.getPositionX(), quest.getPositionY(), quest.getAnswer(), quest.getDirection());
                if (checkCharArr(charArr)) {
                    result = puzzUltil.getCellsToStringArr(charArr, result);
                }
            }
            return result;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * Redirect to website
     * @param url
     */
    public static void openURL(String url) {
        String osName = System.getProperty("os.name");
        try {
            if (osName.startsWith("Windows")) {
                Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
            } else {
                String[] browsers = {"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape"};
                String browser = null;
                for (int count = 0; count < browsers.length && browser == null; count++) {
                    if (Runtime.getRuntime().exec(new String[]{"which", browsers[count]}).waitFor() == 0) {
                        browser = browsers[count];
                    }
                }
                Runtime.getRuntime().exec(new String[]{browser, url});
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error in opening browser" + ":\n" + e.getLocalizedMessage());
        }
    }

    public static void main(String[] args) {
        PuzzleUltil puz = new PuzzleUltil();
        char[][] cells = puz.getCellsByPosisitonAndDirection(4, 4, 0, 3, "Blue", true);
        String[][] res = new String[4][4];
        res = puz.getCellsToStringArr(cells, res);
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                System.out.println("cell: (" + i + ", " + j + "): " + cells[i][j]);
            }
        }
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                System.out.println("cell res: (" + i + ", " + j + "): " + res[i][j]);
            }
        }
    }
}
